package com.kim.rssreader.utils;

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.widget.ImageView;

public class ImageScaleUtil {

	private static float SCREEN_WIDTH = 0;
	private static float SCREENT_HEIGHT = 0;

	private static final int DEFAULT_VERTICAL_MODIFY = 130;

	private static float ZOOM_OUT_MIN_SCALE = 0.5f;//缩小极限系数
	private static float ZOOM_IN_MAX_SCALE = 2.0f;//放大极限系数
	
	public static void initScreenSize(Activity context) {
		Display display = context.getWindowManager().getDefaultDisplay();
		SCREEN_WIDTH = display.getWidth();
		SCREENT_HEIGHT = display.getHeight() - DEFAULT_VERTICAL_MODIFY;
	}

	public static void imageScale(Activity context, ImageView imageView,
			Drawable drawable, Integer horizontalModify, Integer verticalModify) {

		if (drawable == null) {
			return;
		}
		// 扩展到适屏大小
		if (SCREEN_WIDTH == 0 || SCREENT_HEIGHT == 0) {
			initScreenSize(context);
		}

		float screenWidth = SCREEN_WIDTH;
		float screenHeight = SCREENT_HEIGHT;

		if (horizontalModify != null && horizontalModify != 0) {
			// screenWidth = horizontalModify;
		}

		if (verticalModify != null && verticalModify != 0) {
			screenHeight = verticalModify;
		}

		float widthOld = drawable.getIntrinsicWidth();
		float heightOld = drawable.getIntrinsicHeight();

		float scaleWidth = screenWidth / widthOld;
		float scaleHeight = screenHeight / heightOld;

		float scale = (screenWidth - widthOld) < (screenHeight - heightOld) ? scaleWidth
				: scaleHeight;

		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);

		float xTransport = 0.0f;
		float yTransport = 0.0f;

		if (heightOld * scale < screenHeight) {
			yTransport = (screenHeight - (heightOld * scale)) / 2;
		}

		if (widthOld * scale < screenWidth) {
			xTransport = (screenWidth - (widthOld * scale)) / 2;
		}

		matrix.postTranslate(xTransport, yTransport);
		imageView.setImageMatrix(matrix);
	}

	/**
	 * 调整水平移动的步长，不允许超出屏幕
	 * 
	 * @param imageRect 图片的实际位置
	 * @param viewRect view控件的位置
	 * @param oldMoveLength 调整前的步长
	 * @return 调整之后的步长
	 */
	public static int imageFitHMove(Rect imageRect, Rect viewRect,
			int oldMoveLength) {
		int movelength_h_fit = oldMoveLength;

		if (movelength_h_fit <= 0) {
			if (imageRect.right - imageRect.left >= viewRect.width())// 实际的比image宽
			{
				if (imageRect.right + movelength_h_fit <= viewRect.right)// 右边出现空白
				{
					movelength_h_fit = viewRect.right - imageRect.right; // 最大移动距离，为右边到极限
				}
			} else// 实际图像比image窄
			{
				if (imageRect.left + movelength_h_fit < viewRect.left)// 左边超出边界
				{
					movelength_h_fit = viewRect.left - imageRect.left;
				}
			}

		} else// 右移动
		{
			if (imageRect.right - imageRect.left >= viewRect.width())// 实际的比image宽
			{
				if (imageRect.left + movelength_h_fit >= viewRect.left)// 右边会出现空白
				{
					movelength_h_fit = viewRect.left - imageRect.left;
				}
			} else// 实际图像比image窄
			{
				if (imageRect.right + movelength_h_fit > viewRect.right)// 右边超出边界
				{
					movelength_h_fit = viewRect.right - imageRect.right;
				}
			}
		}

		return movelength_h_fit;

	}

	/**
	 * 调整垂直移动的步长，不允许超出屏幕
	 * 
	 * @param imageRect 图片的实际位置
	 * @param viewRect view控件的位置
	 * @param oldMoveLength 调整前的步长
	 * @return 调整之后的步长
	 */
	public static int imageFitVMove(Rect imageRect, Rect viewRect,
			int oldMoveLength) {
		int movelength_v_fit = oldMoveLength;

		// 上移动
		if (movelength_v_fit <= 0) {
			if (imageRect.bottom - imageRect.top >= viewRect.height())// 实际的比image高
			{
				if (imageRect.bottom + movelength_v_fit <= viewRect.bottom)// 下边出现空白
				{
					movelength_v_fit = viewRect.bottom - imageRect.bottom; // 最大移动距离，为下边到极限
				}
			} else// 实际图像比image矮
			{
				if (imageRect.top + movelength_v_fit < viewRect.top)// 上边超出边界
				{
					movelength_v_fit = viewRect.top - imageRect.top;
				}
			}

		} else// 下移动
		{
			if (imageRect.bottom - imageRect.top >= viewRect.height())// 实际的比image高
			{
				if (imageRect.top + movelength_v_fit >= viewRect.top)// 上边会出现空白
				{
					movelength_v_fit = viewRect.top - imageRect.top;
				}
			} else// 实际图像比image矮
			{
				if (imageRect.bottom + movelength_v_fit > viewRect.bottom)// 下边超出边界
				{
					movelength_v_fit = viewRect.bottom - imageRect.bottom;
				}
			}
		}
		return movelength_v_fit;
	}
	
	/**
	 * image图像缩放极限
	 * @param originalRect 原始图片大小
	 * @param currentRect  当前图片大小
	 * @param viewRect image容器大小
	 * @return 缩放系数调整
	 */
	public static float imageZoomScaleLimit(Rect originalRect,Rect currentRect, Rect viewRect) {
		float scale = 1.0f;


		float scaleW = (float)viewRect.width()/(float)originalRect.width();
		float scaleH = (float)viewRect.height()/(float)originalRect.height();
		
		float minscale = (scaleW<scaleH)?scaleW:scaleH;
		float maxscale = (scaleW>scaleH)?scaleW:scaleH;
		minscale = minscale*ZOOM_OUT_MIN_SCALE;
		maxscale = maxscale*ZOOM_IN_MAX_SCALE;
		
		float currentScale =(float)currentRect.width()/(float) originalRect.width();
				
		if(currentScale<minscale){
			scale = minscale/currentScale;
		}
		
		if(currentScale>maxscale){
			scale = maxscale/currentScale;
		}
		
		return scale;
	}
	
	
}
